require 'fileutils'
require 'tempfile'
require File.dirname(__FILE__) + '/.config.rb'


#执行该脚本之前请先确认提交的pod分支代码已经合并到master
puts "打tag之前，请先确认提交的pod分支代码已经合并到pod的master分支(yes/no)"
checkMerge = gets

if checkMerge.include?("n") then
    return
end

#pod增加tag
puts "请输入pod name"
podName = gets
#检查是否存在该pod
podName = podName.chomp
puts("==========我是华丽的分割线==========")
libPath = LOCAL_REP_PATH + '/' + podName
puts "当前发布组件位置：#{libPath}"
isExist = File.directory?(libPath)
if !isExist then
    puts("请确认#{podName}是否正确，如果正确，那么#{podName}还未checkout到本地,请先修改podfile里面该pod使用pod_dev_lib")
else
    puts("#{podName} start tag")

    puts "#{podName}当前已有tag如下："
    tagListLog = `cd;cd #{libPath};git pull;git tag -l`
    puts tagListLog

    puts "请输入tag version"
    tagVersion = gets
    tagVersion = tagVersion.chomp
    
    puts "请输入tag comment"
    tagComment = gets
    tagComment = tagComment.chomp

    puts("==========我是华丽的分割线==========")
    puts("#{podName} spec文件开始修改")

    tagLog = `cd;cd #{libPath};git stash;git checkout master;git pull`
    puts tagLog
    #修改lib spec版本号
    libSpecFilePath = libPath + '/' + podName;
    tempFilePath = podName + '_temp' + '.podspec'
    puts tempFilePath
    t_file = Tempfile.new(tempFilePath)
    libSpecFilePath = libSpecFilePath + '.podspec'
    File.open(libSpecFilePath).each do |line|
        if line.include?("s.version") then
            if !line.include?("=>") then
                t_file.puts "  s.version          = \'#{tagVersion}\'"
                puts line
            else
                t_file.puts line
            end
        else
            t_file.puts line
        end
    end
    t_file.close
    puts t_file.path
    FileUtils.mv(t_file.path, libSpecFilePath)
    puts("#{podName} spec文件修改成功")

    puts("==========我是华丽的分割线==========")
    puts("#{podName} 开始打tag并提交")

    tagLog = `cd;cd #{libPath};git add .;git commit -m "feat:podLibPublish update podspec";git push -u origin master;git tag -a #{tagVersion} -m \"#{tagComment}\";git push origin #{tagVersion}`
    puts tagLog

    if $? != 0
        puts("#{podName} tag #{tagVersion} failed")
    else
        puts("#{podName} tag #{tagVersion} success, 开始同步Spec")

        puts("==========我是华丽的分割线==========")

        #同步到Spec
        localSpecFilePath = LOCAL_SPEC_PATH + '/' + podName + '/'
        specFolderExist = File.directory?(localSpecFilePath)
        if !specFolderExist then
            sepcCreateLog = `cd #{LOCAL_SPEC_PATH};mkdir #{podName}`
            puts sepcCreateLog
        end
        localSpecFilePath = localSpecFilePath + tagVersion + '/'
        tagExist = File.directory?(localSpecFilePath);
        if !tagExist then
            log = `pod repo update #{SPEC_NAME};cd #{LOCAL_SPEC_PATH}/#{podName}/;mkdir #{tagVersion};cp -R #{libSpecFilePath} #{localSpecFilePath}`
            puts log
            commitSpecInfo = "podTagHelp commit #{podName} #{tagVersion}"
            updateSpecLog = `cd #{LOCAL_SPEC_PATH};git add .;git commit -m "#{commitSpecInfo}";git push -u origin master`
            puts updateSpecLog
            if $? != 0 then
                puts("#{commitSpecInfo} failed")
            else
                puts("#{commitSpecInfo} success")
            end
        else
            puts("#{tagVersion}spec 已经存在#{SPEC_NAME}")
        end
    end

    puts("==========我是华丽的分割线==========")
    checkoutBranchLog = `cd;cd #{libPath};git checkout master`
    puts checkoutBranchLog
end
